package com.ihr360.payroll.model.dto.staff;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ihr360.commons.lang.DateUtils;
import com.ihr360.commons.model.entity.StringIdWithTimeAndCompanyId;
import com.ihr360.context.SpringContext;
import com.ihr360.entity.BasicStaffInfoEntity;
import com.ihr360.hibernate.type.encrypt.JsonEncryptType;
import com.ihr360.hibernate.type.json.JsonStringType;
import com.ihr360.payroll.entity.staff.SalaryProfileEntity;
import com.ihr360.payroll.model.dto.salary.convertor.SalaryProfileConvertor;
import com.ihr360.payroll.model.dto.staff.embeddable.SalaryProfile;
import com.ihr360.shared.basicstaffinfo.convertor.SyncedBasicStaffInfoConvertor;
import com.ihr360.shared.basicstaffinfo.dto.SyncedBasicStaffInfo;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import java.util.Date;

/**
 * 员工薪资档案
 */
@Entity
@Table(indexes = {
        @Index(name = "company_staff_idx", columnList = "companyId, staffId,effectiveAt,changeType", unique = true)})
@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "json-encrypt", typeClass = JsonEncryptType.class)
})
public class StaffSalaryProfile extends StringIdWithTimeAndCompanyId {

    public static final class Field {
        public static final String EFFECTIVE_AT = "salaryProfile.effectiveAt";
        public static final String INVALID_AT = "salaryProfile.inValidAt";
        public static final String STAFF_ID = "staff.id";
    }

    /**
     * 员工信息
     */
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "staffId", nullable = false)
    private SyncedBasicStaffInfo staff;


    /**
     * 员工薪资档案信息
     */
    @Embedded
    private SalaryProfile salaryProfile;

    @JsonIgnore
    public BasicStaffInfoEntity getBasicStaffInfoEntity() {
        if (staff == null) {
            return null;
        }
        SyncedBasicStaffInfoConvertor convertor = SpringContext.getBean(SyncedBasicStaffInfoConvertor.class);
        return convertor.convertDtoToEntity(staff);
    }

    @JsonIgnore
    public SalaryProfileEntity getSalaryProfileEntity() {
        if (salaryProfile == null) {
            return null;
        }
        SalaryProfileConvertor convertor = SpringContext.getBean(SalaryProfileConvertor.class);
        return convertor.convertDtoToEntity(salaryProfile);
    }

    public SyncedBasicStaffInfo getStaff() {
        return staff;
    }

    public void setStaff(SyncedBasicStaffInfo staff) {
        this.staff = staff;
    }

    public SalaryProfile getSalaryProfile() {
        return salaryProfile;
    }

    public void setSalaryProfile(SalaryProfile salaryProfile) {
        this.salaryProfile = salaryProfile;
    }


    /**
     * 当前生效：当前天大于等于生效日，小于失效日;失效日>生效日
     *
     * @return
     */
    public boolean isCurrentEffect() {
        Date effectiveAt = salaryProfile.getEffectiveAt();
        Date inValidAt = salaryProfile.getInValidAt();

        Date currentDate = DateUtils.getMinDateInDay(new Date());

        return (DateUtils.isSameDay(currentDate, effectiveAt) || currentDate.after(effectiveAt))
                && (inValidAt == null || currentDate.before(inValidAt) ||  DateUtils.isSameDay(currentDate, inValidAt))
                && (inValidAt == null || effectiveAt.before(inValidAt) || DateUtils.isSameDay(currentDate, inValidAt));
    }

}
